var currentPage = 1;//当前页码
var pageSize = 8;//每页显示的数据量
var totalCount = 0;//总条数
var totalPage = 1;//总页码

var userName = "";
var rname = "";
var brName = "";

function chaxundefangfa() {

    userName = $(".userName").val();
    rname = $(".rname").val();
    brName = $(".brName").val();
    if (userName == "" || userName == null) {
        userName = null;
    }
    if (rname == "" || rname == null) {
        rname = null;
    }
    if (brName == "" || brName == null) {
        brName = null;
    }

    let joinA = {
        userName: userName,
        rname: rname,
        brName: brName,
        currentPage: currentPage,
        pageSize: pageSize
    }
    $.ajaxSettings.async = false;
    $.getJSON("/xm/show", joinA, function (dataA) {
        //console.log(dataA)
        //总条数
        totalCount = dataA.total;
        //总页数
        totalPage = dataA.pages;
        //alert(totalCount);
        //获取回调后渲染数据
        //清空
        $(".table-page tr:gt(0) td").html("&nbsp;");
        $(dataA.list).each(function (i, v) {

            //console.log(v);
            $(".table-page").find("tr").eq(i + 1).find("td").eq(0).text((currentPage - 1) * pageSize + i + 1);
            $(".table-page").find("tr").eq(i + 1).find("td").eq(1).text(v.userCode);
            $(".table-page").find("tr").eq(i + 1).find("td").eq(2).text(v.userName);
            $(".table-page").find("tr").eq(i + 1).find("td").eq(3).text(v.rname);
            $(".table-page").find("tr").eq(i + 1).find("td").eq(4).text(v.brName);
            $(".table-page").find("tr").eq(i + 1).find("td").eq(5).html('<div class="layui-btn-group">' +
                '<a href="/xm/yonghuxiugai/' + v.id + '" class="layui-btn  layui-btn-normal">' + '变更' + '</a>' +
                '<a href="/xm/yonghumingxi/' + v.id + '" class="layui-btn layui-btn-normal">' + '明细' + '</a>' +
                '<button type="button" class="layui-btn layui-btn-normal layui-btn-danger" onclick="del(' + v.id + ',this)">删除</button>' +
                '</div>');
        })
    })
    $.ajaxSettings.async = true;
    //以下判断防止“0数据”，出现1/0的情况！收尾工作
    if (totalCount == 0) {
        totalPage = 1;
        $(".showPage").text(currentPage + "/" + totalPage);
        alert("抱歉，未找到符合条件的数据！");
    }
    if (currentPage > totalPage) {
        currentPage = totalPage;
        $(".showPage").text(currentPage + "/" + totalPage);
    }
    showPage();
}

function del(id, thisA) {
    let con = confirm("确定要删除吗？")
    if (con == true) {
        let jsonA = {
            id: id
        }
        $.ajaxSettings.async = false;
        $.getJSON("/xm/del", jsonA, function (dataA) {
            if (dataA = true) {
                alert("删除成功")
                chaxundefangfa()
            } else {
                alert("删除失败")
                return;
            }
        })
    } else {
        return;
    }
    $.ajaxSettings.async = true;
}


function showPage() {
    $(".showPage").text("第" + currentPage + "页 / 共" + totalPage + "页");
}

//下一页
function next() {
    currentPage++;
    if (currentPage > totalPage) {
        currentPage = totalPage;
        return;
    }
    chaxundefangfa();
}


//上一页
function prev() {
    currentPage--;
    if (currentPage < 1) {
        currentPage = 1;
        return;
    }
    chaxundefangfa();
}

//首页
function start() {
    currentPage = 1;
    chaxundefangfa();
}

//尾页
function end() {
    currentPage = totalPage;
    chaxundefangfa();
}



// function adduser(){
//     $(".userrole option:selected").val();
// }

function fn() {
    //var name=$(".userrole").find("option:selected").text(); //获取文本值
    var name = $(".userrole").find("option:selected").val();  //获取value值
    if (name !== null) {
        version = name;
    }
}

function addUser() {
    let userCode=$(".userCode").val();
    let userName=$(".userName").val();
    let userPwd=$(".userPwd").val();
    var rId=$(".rId").find("option:selected").val(); //获取文本值

    let json={
        userCode:userCode,
        userName:userName,
        userPwd:userPwd,
        rId:rId,
    }
    $.ajaxSettings.async = false;
    $.post("/xm/add",json,function(data){
        if(data==true){
            alert("添加成功");
            location.href="/xm/guanli"
        }else{
            alert("添加失败");
        }
    })
    $.ajaxSettings.async = true;
}
function upd() {
    let userCode=$(".userCode").val();
    let userName=$(".userName").val();
    let userPwd=$(".userPwd").val();
    let id=$(".id").val();

    var rId=$(".rId").find("option:selected").val(); //获取文本值
    let json={
        id:id,
        userCode:userCode,
        userName:userName,
        userPwd:userPwd,
        rId:rId,
    }
    $.ajaxSettings.async = false;
    $.post("/xm/upd",json,function(data){
        if(data==true){
            alert("修改成功");
            location.href="/xm/guanli"
        }else{
            alert("修改失败");
        }
    })
    $.ajaxSettings.async = true;
}

function isUniqueUserCode(a) {
    //获取页面输入框输入的用户名

    var userCode=$("#userCode").val();
    //请求controller判断用户名的唯一性
    let json={
        userCode:userCode
    }
  $.getJSON("/xm/isUniqueUserCode",json,function(data){
      if(data==false){
          alert("用户编码已存在")
          return false;

      }else{
          return true;

      }
  })
}